
define(function(require, exports, module) {
    var $=require('zepto');
    api=require('api');
    cn=require('conment');
    pop=require('popup');
    template=require('template');
    require('jqpage');
    lg=require('login');
    module.exports = {
        int:{
            wt_pageSize:'10',
            wt_pageNo:1,
            tab_type:'mrmc',
        },
        codeList:'',
        getNowFormatDate:function () {
            var date = new Date();
            var seperator1 = "-";
            var year = date.getFullYear();
            var month = date.getMonth() + 1;
            var strDate = date.getDate();
            if (month >= 1 && month <= 9) {
                month = "0" + month;
            }
            if (strDate >= 0 && strDate <= 9) {
                strDate = "0" + strDate;
            }
            var currentdate = year + seperator1 + month + seperator1 + strDate;
            return currentdate;
        },
        holdReady:function (num) {
            var _this=this;
            _this.codeList='';
            cn.Ajax(api.dealList,{
                Length:_this.int.wt_pageSize,
                page:num,
                tab:_this.int.tab_type,
            },function(data){

                var json = {
                    data:[]
                };
                json.data = data;
                json.data.time=_this.getNowFormatDate();
                var result = TrimPath.processDOMTemplate("tempp", json);
                document.getElementById("show-ewm-tidp").innerHTML = result;
                $.jqPaginator('#pagination1', {
                    totalPages:  1,
                    currentPage: data.page,
                    prev: '<li class="prev"><a href="javascript:;">上一页</a></li>',
                    next: '<li class="next"><a href="javascript:;">下一页</a></li>',
                    page: '<li class="page"><a href="javascript:;">{{page}}</a></li>',
                    onPageChange: function (num, type) {
                        if(type == 'change'){
                            _this.holdReady(num)
                        }
                    }
                });
                for(var i=0;i<data.list.length;i++){
                    _this.codeList+=data.list[i].code+','
                }
                $('.sell-btn').on('click',function () {
                    _this.sellHold($(this));
                })
                // console.log(_this.codeList)
            })

        },

        timeIndex_market:function () {
            var _this=this;
            cn.Ajax(api.url_stock.moreMarket,{
                codes :_this.codeList,
            },function(d){
                var list=d.data;
                $.each(list,function(i,l,array){
                    $('.code'+l.code+' .price').text(l.tradePrice);
                });
                for(var j=0;j<$('#sell-list>li').length;j++){
                    var state=$('#sell-list>li').eq(j).find('.bindMsg').data('state');
                    var num=$('#sell-list>li').eq(j).find('.bindMsg').data('num');
                    var price=parseFloat($('#sell-list>li').eq(j).find('.price').text());
                    var buyprice=parseFloat($('#sell-list>li').eq(j).find('.bindMsg').data('buyprice'));
                    if(state>=4){
                        $('#sell-list>li').eq(j).find('.baifenbi').text(parseFloat((price-buyprice)/buyprice*100).toFixed(2)+'%');
                        $('#sell-list>li').eq(j).find('.profitN').text('盈亏：'+parseFloat((price-buyprice)*num).toFixed(2)+'元');
                        $('#sell-list>li').addClass(price>=buyprice?'red':'green').removeClass(price>=buyprice?'green':'red')
                    }
                }
                $('.baifenbi').text();

                setTimeout(function () {
                    _this.timeIndex_market()
                },3000)
            },'post')
        },
        loadTime:function () {
            var _this=this;
            if(_this.codeList.length!=0){
                _this.timeIndex_market()
            }else{
                setTimeout(function () {
                    _this.loadTime()
                },1000)
            }
        },
        sellHold:function (t) {
            var _this=this;
            var arr=t.data('arr');
            $('#investorHolderCode').text(arr.investorHolderCode);
            $('#dadangname').text(arr.investorDisplayName);
            $('#model-name').text(arr.name);
            $('#model-code').text(arr.code);
            $('#model-time').text(arr.applyTimeStr);
            $('#model-amount').text(arr.amount);
            $('#model-num').text(arr.buyQuantityPlan);
            $('#model-price').text(arr.buyPricePlan);
            $('#model-typeStr').text(arr.typeStr);
            $('#model-profitPercent').text(arr.profitPercent*100+'%');
            $('#model-lowPercent').text(arr.lowPercent*100+'%');
            $('#model-amountBond').text(arr.amountBond);
            $('#model-amountRate').text(arr.amountRate);
            pop.popup_html({
                id:'#popup-sell',
            });
            $('#popup-sell-btn').on('click',function () {
                _this.sellDeal(arr.id)
            })
        },
        sellDeal:function (id) {
            var _this=this;
            cn.Ajax(api.sell,{
                id : id,
            },function(d){
                if(d.flag==1){
                    pop.popup_success_msg({
                        text:d.message,
                        id:'#popup-p-error',
                        cBack:{
                            text:'确定',
                            callBack:function () {
                                location.reload()
                                return false;
                            }
                        },
                    })
                }else{
                    pop.popup_err_msg({
                        id:'#popup-p-error',
                        text:d.message
                    })
                }

            },'post')
        },
        history:function () {
            var _this=this;
            lg.userInfo();
            _this.int.tab_type='ykfp';
            _this.holdReady(_this.int.wt_pageNo);
            
        },

        init:function(){
            var _this=this;
            lg.userInfo();
            _this.loadTime();
            _this.int.tab_type='mrmc';
            _this.holdReady(_this.int.wt_pageNo);

        },

    }
});